package io.renren.modules.jxgkquestion.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.jxgk.utils.UUidCreater;
import io.renren.modules.jxgkquestion.entity.QuestionBankEntity;
import io.renren.modules.jxgkquestion.entity.ScoreDataEntity;
import io.renren.modules.jxgkquestion.entity.TestBankEntity;
import io.renren.modules.jxgkquestion.service.QuestionBankService;
import io.renren.modules.jxgkquestion.service.TestBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("QuestionBank/")
public class QuestionBankController {

    @Autowired
    QuestionBankService questionBankService;
    @Autowired
    TestBankService testBankService;

    @RequestMapping("Select")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<QuestionBankEntity> cse = questionBankService.queryList(query);
        int total = questionBankService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(cse, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
    @RequestMapping("/Delete")
    public R delete(@RequestBody String[] indexs){
        questionBankService.deleteBatch(indexs);
        return R.ok();
    }
    @RequestMapping("/Insert/{testId}")
    public R save(@RequestBody QuestionBankEntity ste,@PathVariable("testId") String testId){
        UUidCreater UUid=new UUidCreater();
        String uid=UUid.getUUID();
        ste.setQuestionId(uid);
        questionBankService.save(ste);//保存到题库

        TestBankEntity result=new TestBankEntity();
        TestBankEntity testInfo=testBankService.queryObject(testId);
        result.setTestId(testId);
        result.setTestQuestionId(uid);
        result.setTestName(testInfo.getTestName());
        result.setTestSubject(ste.getQuestionSubject());
        result.setTestTime(testInfo.getTestTime());
        testBankService.save(result);

        return R.ok();
    }
    @RequestMapping("/Info/{index}")
    public R info(@PathVariable("index") String index){
        QuestionBankEntity cse = questionBankService.queryObject(index);
        return R.ok().put("QuestionBank", cse);
    }
    @SysLog("修改用户")
    @RequestMapping("/Update")
    public R update(@RequestBody QuestionBankEntity cse){
        questionBankService.update(cse);
        return R.ok();
    }
    @RequestMapping("/SubjectInfo/")
    public List<String> subjectInfo(){
        List<String> subjectInfo=questionBankService.findAllSubjectType();
        subjectInfo.add(0,"公共知识类");
        return subjectInfo;
    }
    @RequestMapping("/QuestionInfo/{departmentType}")
    public List<QuestionBankEntity> QuestionInfo(@PathVariable("departmentType") String departmentType){
        return   questionBankService.findQuestionByDepartmentType(departmentType);
    }
    @RequestMapping("/UpdateScore")
    public void updateScore(@RequestBody ScoreDataEntity sde){
       ScoreDataEntity scoreInfo=questionBankService.findCurrentScoreInfoByIdAndSubject(sde.getScoreStudentId(),sde.getScoreSubjectName());

        int scoreAdder,amountAdder;
        int currentScore,currentAmount;
        int scoreResult,amountResult;
        double rateResult;
        if(scoreInfo==null){
          amountResult=5;
          scoreResult=Integer.parseInt(sde.getScoreAmount());
          rateResult=(double) scoreResult/(double) amountResult;
       }else{
           currentScore=Integer.parseInt(scoreInfo.getScoreAmount());
           currentAmount=Integer.parseInt(scoreInfo.getScoreQuestionAmount());
           scoreAdder=Integer.parseInt(sde.getScoreAmount());
           amountAdder=5;//默认每次做五道题
           amountResult=currentAmount+amountAdder;
           scoreResult=currentScore+scoreAdder;
           rateResult=(double) scoreResult/(double) amountResult;
       }
       sde.setScoreQuestionAmount(amountResult+"");
        sde.setScoreQuestionRate(rateResult+"");
       sde.setScoreAmount(scoreResult+"");
       questionBankService.updateScoreAmount(sde);
    }
}